﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using Competition.Application.DTO;
using Competition.Application.Interfaces;
using Competition.Domain;
using Competition.Domain.Entity;
using Competition.Domain.ValueObjects;
using Microsoft.EntityFrameworkCore;

namespace Competition.Application
{
    public class IntroductionService:BaseService<Introduction>, IIntroductionService
    {
        public IntroductionService(AppDbContext dbContext) : base(dbContext)
        {
        }

        public async Task<List<IntroductionDto>> FindListAsync(IntroductionEnum classId)
        {
            var models = this.GetService<Introduction>().FindList(i=>i.ClassId==classId,nameof(Introduction.AddTime));
            List<IntroductionDto> dtos = await models.Select(model => new IntroductionDto()
            {
                Id = model.Id,
                Title = model.Title,
                Description = model.Description,
                AddTime = model.AddTime
            }).ToListAsync();
            return dtos;
        }
        public async Task<IntroductionDto> FindAsync(int id)
        {
            var model = await this.GetService<Introduction>().FindAsync(n => n.Id == id);
            var dto = new IntroductionDto()
            {
                Description = model.Description,
                AddTime = model.AddTime,
                Title = model.Title,
                Id = model.Id,
                Html = model.Html,
                Text = model.Text,
                Status = model.Status,
                ClassId = model.ClassId
            };
            return dto;
        }

        public async Task<int> AddAsync(IntroductionDto dto)
        {
            var  model = new Introduction()
            {
                Html = dto.Html,
                AddTime = DateTime.Now,
                Description = dto.Description,
                Title = dto.Title,
                ClassId = dto.ClassId,
                Status = dto.Status,
                Text = dto.Text
            };
            model = await this.GetService<Introduction>().AddAsync(model);
            return model.Id;
        }

        public async Task<bool> UpdateAsync(IntroductionDto dto)
        {
            var model = await this.GetService<Introduction>().FindAsync(i => i.Id == dto.Id);
            model.Html = dto.Html;
            model.Description = dto.Description;
            model.Title = dto.Title;
            model.ClassId = dto.ClassId;
            model.Status = dto.Status;
            model.Text = dto.Text;
            bool b = await this.GetService<Introduction>().UpdateAsync(model);
            return b;
        }
        /// <summary>
        /// 根据条件列表
        /// </summary>
        /// <param name="pageIndex">页面索引</param>
        /// <param name="pageSize">页面大小</param>
        /// <returns>列表</returns>
        public async Task<(PageDto pageDto, List<IntroductionDto> dtos)> FindListAsync(int pageIndex, int pageSize)
        {
            Expression<Func<Introduction, bool>> whereLamdba = news => true;

            var v = this.GetService<Introduction>().FindPageList2(pageIndex, pageSize, whereLamdba, nameof(Introduction.AddTime));
            List<IntroductionDto> dtos = await v.list.Select(model => new IntroductionDto()
            {
                Id = model.Id,
                AddTime = model.AddTime,
                Title = model.Title,
                Status = model.Status,
                Html = model.Html,
                Description = model.Description,
                ClassId = model.ClassId
            }).ToListAsync();

            var pageDto = new PageDto()
            {
                PageSize = pageSize,
                PageIndex = pageIndex,
                NextPage = v.nextPage,
                TotalRecord = v.totalRecord,
                PrevPage = v.prevPage,
                PageCount = v.pageCount
            };
            return (pageDto, dtos);
        }
        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<bool> DeleteAsync(int id)
        {
            var model = await this.GetService<Introduction>().FindAsync(b => b.Id == id);
            return await this.GetService<Introduction>().DeleteAsync(model);
        }
    }
}